﻿using System.Windows.Controls.Primitives;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Input;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace WpfToolkit
{
    /// <summary>
    /// 验证数据
    /// </summary>
    /// <remarks>add by yfc 2019.11.19</remarks>
    public class RegulateTextBox : TextBox, ICommandSource
    {
        #region PlacementEx

        public EnumPlacement PlacementEx
        {
            get { return (EnumPlacement)GetValue(PlacementExProperty); }
            set { SetValue(PlacementExProperty, value); }
        }

        public static readonly DependencyProperty PlacementExProperty =
            DependencyProperty.Register("PlacementEx", typeof(EnumPlacement), typeof(RegulateTextBox)
                , new PropertyMetadata(EnumPlacement.RightCenter, PlacementExChangedCallback));

        private static void PlacementExChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Poptip poptip = d as Poptip;
            if (poptip != null)
            {
                EnumPlacement placement = (EnumPlacement)e.NewValue;
                switch (placement)
                {
                    case EnumPlacement.LeftTop:
                        break;
                    case EnumPlacement.LeftBottom:
                        break;
                    case EnumPlacement.LeftCenter:
                        break;
                    case EnumPlacement.RightTop:
                        break;
                    case EnumPlacement.RightBottom:
                        break;
                    case EnumPlacement.RightCenter:
                        break;
                    case EnumPlacement.TopLeft:
                        break;
                    case EnumPlacement.TopCenter:
                        poptip.Placement = PlacementMode.Top;
                        break;
                    case EnumPlacement.TopRight:
                        break;
                    case EnumPlacement.BottomLeft:
                        break;
                    case EnumPlacement.BottomCenter:
                        break;
                    case EnumPlacement.BottomRight:
                        break;
                    default:
                        break;
                }
            }
        }

        #endregion

        #region DependencyProperty

        #region Pattern

        public string Pattern
        {
            get { return (string)GetValue(PatternProperty); }
            set { SetValue(PatternProperty, value); }
        }

        public static readonly DependencyProperty PatternProperty =
            DependencyProperty.Register("Pattern", typeof(string), typeof(RegulateTextBox), new PropertyMetadata(string.Empty));

        #endregion


        #region ErrorContent

        public string ErrorContent
        {
            get { return (string)GetValue(ErrorContentProperty); }
            set { SetValue(ErrorContentProperty, value); }
        }

        public static readonly DependencyProperty ErrorContentProperty =
            DependencyProperty.Register("ErrorContent", typeof(string), typeof(RegulateTextBox), new PropertyMetadata(null));

        #endregion   

        #region ErrorContentTemplate

        public DataTemplate ErrorContentTemplate
        {
            get { return (DataTemplate)GetValue(ErrorContentTemplateProperty); }
            set { SetValue(ErrorContentTemplateProperty, value); }
        }

        public static readonly DependencyProperty ErrorContentTemplateProperty =
            DependencyProperty.Register("ErrorContentTemplate", typeof(DataTemplate), typeof(RegulateTextBox));

        #endregion

        #region ErrorBackground

        public Brush ErrorBackground
        {
            get { return (Brush)GetValue(ErrorBackgroundProperty); }
            set { SetValue(ErrorBackgroundProperty, value); }
        }

        public static readonly DependencyProperty ErrorBackgroundProperty =
            DependencyProperty.Register("ErrorBackground", typeof(Brush), typeof(RegulateTextBox));

        #endregion
        public static readonly DependencyProperty CommandProperty =
                DependencyProperty.Register(
                        "Command",
                        typeof(ICommand),
                        typeof(RegulateTextBox),
                        new FrameworkPropertyMetadata((ICommand)null));

        /// <summary>
        /// Get or set the Command property
        /// </summary>
        [Bindable(true), Category("Action")]
        [Localizability(LocalizationCategory.NeverLocalize)]
        public ICommand Command
        {
            get
            {
                return (ICommand)GetValue(CommandProperty);
            }
            set
            {
                SetValue(CommandProperty, value);
            }
        }

        /// <summary>
        /// The DependencyProperty for the CommandParameter
        /// </summary>
        public static readonly DependencyProperty CommandParameterProperty =
                DependencyProperty.Register(
                        "CommandParameter",
                        typeof(object),
                        typeof(RegulateTextBox),
                        new FrameworkPropertyMetadata((object)null));
        /// <summary>
        /// Reflects the parameter to pass to the CommandProperty upon execution.
        /// </summary>
        [Bindable(true), Category("Action")]
        [Localizability(LocalizationCategory.NeverLocalize)]
        public object CommandParameter
        {
            get
            {
                return GetValue(CommandParameterProperty);
            }
            set
            {
                SetValue(CommandParameterProperty, value);
            }
        }

        /// <summary>
        ///     The DependencyProperty for Target property
        ///     Flags:              None
        ///     Default Value:      null
        /// </summary>
        public static readonly DependencyProperty CommandTargetProperty =
                DependencyProperty.Register(
                        "CommandTarget",
                        typeof(IInputElement),
                        typeof(RegulateTextBox),
                        new FrameworkPropertyMetadata((IInputElement)null));
        /// <summary>
        ///     The target element on which to fire the command.
        /// </summary>
        [Bindable(true), Category("Action")]
        public IInputElement CommandTarget
        {
            get
            {
                return (IInputElement)GetValue(CommandTargetProperty);
            }
            set
            {
                SetValue(CommandTargetProperty, value);
            }
        }

        #endregion

        #region Constructors

        static RegulateTextBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(RegulateTextBox), new FrameworkPropertyMetadata(typeof(RegulateTextBox)));
        }

        #endregion

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            this.PreviewKeyUp += RegulateTextBox_PreviewKeyUp;
            this.PreviewMouseUp += RegulateTextBox_PreviewMouseUp;
            this.GotFocus += RegulateTextBox_GotFocus; ;
            this.IsEnabledChanged += RegulateTextBox_IsEnabledChanged;
        }

        private void RegulateTextBox_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            try
            {
                TextBox t = sender as TextBox;
                if (t.IsEnabled)
                {
                    t.Foreground= new SolidColorBrush((Color)ColorConverter.ConvertFromString("Black"));
                }
                else 
                {
                    t.Foreground =new SolidColorBrush((Color)ColorConverter.ConvertFromString("Gray"));
                }
            }
            catch (System.Exception)
            {
            }
        }

        private void RegulateTextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                TextBox t = sender as TextBox;
                t.Select(t.Text.Length, 0);
            }
            catch (System.Exception)
            {
            }           
        }

        private void RegulateTextBox_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            try
            {
                TextBox t = sender as TextBox;               
                t.SelectAll();
            }
            catch (System.Exception)
            {
            }
        }

        private void RegulateTextBox_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            ExecuteCommandSource(this);
        }

        /// <summary>
        ///     Executes the command on the given command source.
        /// </summary>
        internal static void ExecuteCommandSource(ICommandSource commandSource)
        {
            ICommand command = commandSource.Command;
            if (command != null)
            {
                object parameter = commandSource.CommandParameter;
                command.Execute(parameter);
            }
        }
    }
}
